package com.xing.binbackend.pattern.adapter24.ecommerce;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * payment:
 * wechat:
 * version: legacy # 或 new
 * new-config:
 * app-id: "wx123456"
 * mch-id: "987654"
 * legacy-config:
 * api-key: "old_key_abc"
 */
@Configuration
public class PaymentConfig {
    // 根据配置动态选择实现版本
    @Value("${payment.wechat.version:new}")
    private String version;

    @Bean
    //  @Resource字段注入 + @Bean方法会导致混乱，配置类中更推荐通过方法参数获取Bean（参数会自动装配）
    public IPayment wechatPayService(WechatAdapter adapter, WechatNewAdapter adapterNew) {
        // Spring会自动注入@Component的Bean
        switch (version) {
            case "old":
                // 使用适配器包装旧版
                return adapter;
            case "new":
                // 用新版
                return adapterNew;
            default:
                throw new IllegalArgumentException("Invalid version");
        }
    }
}
